package com.vxiong.vxdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.vxiong.vxdemo.R;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Samson on 16/8/27.
 */
public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.ViewHolder>
        implements View.OnClickListener{
    private Context mContext;
    private List<HashMap<String, String>> datas;

    public MainListAdapter(Context context, List<HashMap<String, String>> list) {
        this.mContext = context;
        this.datas = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // 给ViewHolder设置布局文件
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_main_card_view, viewGroup, false);
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        // 给ViewHolder设置元素
        viewHolder.itemView.setTag(i);
        viewHolder.textView.setText(datas.get(i).get("name"));
    }

    @Override
    public int getItemCount() {
        // 返回数据总数
        return datas == null ? 0 : datas.size();
    }

    @Override
    public void onClick(View view) {
        Log.e("adapter","点击");
    }


    // 重写的自定义ViewHolder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public ViewHolder(View itemview) {
            super(itemview);
            textView = (TextView) itemview.findViewById(R.id.tv);
        }
    }
}